using System;

using libsecondlife;

using Feathertail.Tracker;

namespace Feathertail.Tracker.Filters
{
    public class NameFilter : ObjectFilter
    {
        public enum TextMatchType { Equals, StartsWith, EndsWith, Contains };
        private string _Text;
        private TextMatchType _Type;
        private bool _CaseSensitive = false;

        public NameFilter(string search, TextMatchType type, bool caseSensitive)
        {
            _Type = type;
            _CaseSensitive = caseSensitive;

            if (_CaseSensitive)
            {
                _Text = search;
            }
            else
            {
                _Text = search.ToLower();
            }
        }

        public override bool IncludeObject(SecondLife Client, LLObject prim)
        {
            if (prim.PropertiesFamily.Name == null)
            {
                return prim.PropertiesFamily.Name == _Text;
            }

            string primName = prim.PropertiesFamily.Name;
            if (!_CaseSensitive)
            {
                primName = primName.ToLower();
            }

            switch (_Type)
            {
                case TextMatchType.Contains:
                    return primName.Contains(_Text);
                case TextMatchType.Equals:
                    return primName.Equals(_Text);
                case TextMatchType.StartsWith:
                    return primName.StartsWith(_Text);
                case TextMatchType.EndsWith:
                    return primName.EndsWith(_Text);
            }

            return false;
        }
    }
}
